<!-- 这个是网页版 -->
<!DOCTYPE html>
<html>
<meta name='viewport' content='initial-scale=1,user-scalable=yes' class="_PDict">
<head>
<style>
	mark {background: yellow; }
	mark.current {
		background: orange;
		box-shadow: inset 0px 0px 0px .5px #ff0000;
	}
	.highlight { background-color: yellow; }
	.PLOD_UL{border-bottom:2.5px solid black}
	img{max-width:100%;}
	html, body {
		-moz-user-select:text;
		-webkit-user-select:text;
		-ms-user-select:text;
		-khtml-user-select:text;
		user-select:text;
	}
</style>
<style id='_pd_mh'>
	
</style>

<script>
var debug = function(a,b,c,d,e){var t=[a,b,c,d,e];for(var i=5;i>=0;i--){if(t[i]===undefined)t[i]='';else break}console.log("%c 子页面 ","color:#333!important;background:#b0f7ce;",t[0],t[1],t[2],t[3],t[4])};
(function(){
var contentUrl = location.origin+location.pathname, contentHash = contentUrl+'#', baseEntryUrl, baseSoundUrl
	, w=window, pw=parent, _app=w.app;
debug("frameElement::", frameElement, contentUrl);
function ge(e){return document.getElementById(e)};
if(frameElement) {
	w._merge={};
	w.edEtJts=function(a, b, c){
		if(!_merge[b]) {
			(b.d?document:w).addEventListener(a, b, c);
			_merge[b] = 1;
		}
	};
}
w.shzh=0;
var remarked=false, withSel;
w.addEventListener('load',wrappedOnLoadFunc,false);
w.addEventListener('click',wrappedClickFunc);
w.addEventListener('mousedown',wrappedDown);
w.addEventListener('touchstart',wrappedDown);
w.addEventListener('touchend',function(){
	if(frameElement) {
		pw.postOnClick(frameElement.inf);
	}
});
//w.addEventListener('scroll',wrappedScroll,true);

// function wrappedScroll(e){
// 	debug(e);
// 	document.body.scrollTop=0;
// 	e.preventDefault();
// 	e.stopPropagation();
// }
var de;
function pageScroll(e){
	var k=0,l=de.scrollLeft,wd=w.innerWidth;
	if(l>0) k=1;
	var de_scrollWidth = frameElement?de.scrollWidth:de.clientWidth;
	if(Math.ceil(l+0.5+wd)<de_scrollWidth) k|=2;
	//debug('onscroll', k, l+0.2+wd, '<', de_scrollWidth);
	if((pw.scrollLck||0)!=k) _app.scrollLck(sid.get(), pw.scrollLck=k);
}
function wrappedOnLoadFunc(){
	debug('wrappedOnLoadFunc');
	w.addEventListener('click',pw.tpshc); //wtf??
	// //document.body.style.fontSize=35+'px';
	// highlight(null);
	// //debug('frameAt !! '+frameAt);
	// if(_onloadchecks && _onloadchecks.length)
	// for(var i=0;i<_onloadchecks.length;i++){
	// 	_onloadchecks[i]();
	// }
	//debug("wrappedOnLoadFunc::", frameElement.tag, frameElement.u, location.href);
	var fe=frameElement;
	if(pw.processPage) pw.processPage(fe);
	if(fe) {
		debug('mdpage loaded', fe.dictInfo.name, 'tpshc!=null:'+pw.tpshc!=null);
		var inf=fe.dictInfo;
		if(pw) {
			// w.addEventListener('click',pw.tpshc);
			for(var i=0,j;j=pw._merge[i++];) {
				edEtJts(j[0], j[1], j[2])
			}
			// if(pw.paddingLeft) {
			// 	document.body.style.paddingLeft = pw.paddingLeft;
			// }
		}
		if(!fe.scrollExpand) {
			if(inf.lastX || inf.lastY)
				document.documentElement.scrollTo(inf.lastX, inf.lastY);
		}
		if(fe.u) {
			if(fe.u.startsWith(location.href)) {
				location.href=fe.u;
			}
			fe.u=0;
		}
		if(pw.bOnceHighlighted) {
			console.log('子页面高亮开始!!!');
			pw.MarkFrame(fe);
		}
		//debug('needs img loader', inf.img, inf.name);
		if(_app && inf.img) {
			pw.LoadIMG(inf);
		}
		ge('_pd_mh').innerText ='html, body {min-height: unset!important;}';
	} else {
		if(_app){
			w.shzh=_app.rcsp(sid.get());
			var lnks = document.links;
			for(var i=0,max=Math.min(lnks.length,10);i<max;i++) {
				if(lnks[i].href.startsWith("sound")) {
					lnks=1; break;
				}
			} 
			_app.maySound(sid.get(), lnks===1);
		}
	}
	if(!remarked) {
		if(fe) {
			restoreMarks(pw); 
		} else if(_app) { // 暂时仅为app内部开启笔记
			loadJs('/MdbR/annot.js', function(){restoreMarks(w)})
		}
		remarked = true;
	}
	//todo 处理背景色？
	if(_app) {
		de = document.documentElement;
		//if(frameElement) 
		document.body.onscroll = pageScroll;
	}
}

function loadJs(url,callback){
	var e=document.createElement('script');
	e.type="text/javascript";
	e.onload=callback;
	e.src=url;
	document.body.appendChild(e);
}

function restoreMarks(pw) {
	var url = ''+location.href, idx = url.indexOf('_'), multi=url.indexOf('_', idx+1) > 0;
	var t = _app.remarkByUrl(sid.get(), url, w.pos||0);
	debug('remarkByUrl???', url, t);
	if(multi) {
		var frames = document.getElementsByClassName('_PDict_body'), map=[];
	 	for(var i=0,f;f=frames[i++];) {
			map[f.getAttribute('pd_pos')] = f;
		}
		t = t.split('\t\n\0'); // \n\n\n
		debug('remarkByUrl::multi::', t, 'map=',map);
	 	for(var i=0,f,n;n=t[i];i+=2) {
			f = map[t[i+1]];
			if(f) {
				debug('remark=', t[i+1], f);
				remark(n, f, document, pw); 
			}
		}
	} else {
		remark(t, document.body, document, pw); 
	}
}

function remark(t, rootNode, doc, w){
    w.RestoreMarks(t, rootNode, doc);
}

w.markPage = function(tcn) {
	var s = w.getSelection();
	if(s && !s.isCollapsed) {
		var wh = whereSelection();
		MakeMark(tcn, wh[2], document, wh[0], wh[1])
	}
}

w.expUrl = function() {
	return url.substring(url.lastIndexOf('/',idx));
}

w.whereSelection = function(){
	var url = ''+location.href, idx = url.indexOf('_'), multi=url.indexOf('_', idx+1) > 0;
	var pos, did = did = url.substring(url.lastIndexOf('/',idx)+1, idx), rootNode=document.body;
	if(multi) {
		var n = getSelection().getRangeAt(0).startContainer;
		while(n=n.parentNode) {
			if(n.classList.contains('_PDict_body')) {
				pos = parseInt(n.getAttribute('pd_pos'))
				rootNode = n;
				break;
			}
		}
	} else {
		//todo handle ? parms
		pos = w.pos;
		if(pos==undefined) pos = pw.decodePosid(url.substring(idx+1));
	}

	debug('whereSelection::', pos, did, url.substring(idx+1));
	return [pos, did, rootNode];

}

w.loadVI = function(pos){
	//111
	debug('hi !! loadVI/'+pos);
	var req=new XMLHttpRequest();
	req.open('POST','VI/'+pos);
	req.responseType='text';
	req.onreadystatechange=function(e) {
		if(req.readyState == 4 && req.status==200) {
			debug(req.responseText);
			var vc = JSON.parse(req.responseText);
			if(vc.JS){
				eval(vc.JS)
			}
		}
	};
	req.send(null);
}
var _onloadchecks;
var frameAt;
var audio;
var regHttp=new RegExp("^https?://");
var regPdf=new RegExp("^pdf://");
var regSound=new RegExp("^sound://");
var stopLnk, nxtEntry;

function hiPlaySound(e){
	e=e.target;
	while(e && !e._pd_vc) e=e.parentNode;
	if(e) {
		var vc = baseSoundUrl+e.getAttribute('href').substring(8);
		debug("subpage hijacked sound playing : ", vc);
		if(audio) audio.pause();
		else audio = new Audio();
		audio.src = vc;
		var pro = audio.play();
		pro.catch(function(error){
			if(((error+'').indexOf('found'))>0) {
				//debug("播放失败.. ");
				if(_app) location.href = 'sound://happy.mp3'
				//else todo 使得 mdbr/sounds/ 等效于 sounds:// ……
			}
		});
	}
}

function popupEntry(e){
	if(pw._touchtarget_lck) return true;
	var t;
	var p=e.path;
	if(!p && e.composedPath) p=e.composedPath();
	for(var i=0;i<10 && (t=p[i]);i++)
		if(t.href!==undefined)
			break;
	debug('popupEntry', t)
	if(t.href.startsWith(contentHash)) {
		location.href = t.href;
		return true;
	}
	if(_app && _app.shouldPopupEntry()) 
	{
		_app.popupEntry(sid.get(), t.href);
		stopLnk = 1;
		return true;
	}
	return false;
}

function getId(){
	return frameElement?frameElement.dictInfo.id:''
}
function entryPop(t){
	debug("entryPop on node::", t, t.href);
	if(_app && _app.entryPopup(sid.get(), getId())/*  && frameElement */) {
		if(t.onclick!==popupEntry) {
			t.rawClick=t.onclick;
			t.onclick=popupEntry; // 选择在新窗口打开！
		}
		nxtEntry=0;
	} else {
		if(t.onclick==popupEntry) t.onclick=t.rawClick;
		nxtEntry=t; // 下一刻在此页面打开！
	}
}

function baseEntry() {
	if(!baseEntryUrl) {
		var tar=contentUrl, tmp=location.pathname.startsWith('/content/');
		if(tmp) {
			tar = tar.replace('/content/','/base/');
			tmp = tar.indexOf('_');
			if(tmp>0) tar = tar.slice(0, tmp);
		} else {
			tmp = tar.indexOf('/entry/');
			if(tmp>0) tar = tar.slice(0, tmp);
		}
		baseSoundUrl = tar+'/';
		baseEntryUrl = tar+'/entry/';
	}
	return baseEntryUrl;
}

function wrappedDown(e){
	debug('wrappedDown', e);
	var t;
	if(pw.scrollLck) pageScroll();
	var p=e.path;
	if(!p && e.composedPath) p=e.composedPath();
	for(var i=0;i<10 && (t=p[i]);i++)
		if(t.href!==undefined)
			break;
	if(!pw._touchtarget_lck && e.touches && e.touches.length==1){
		pw._touchtarget = e.touches[0].target;
		pw.subw = w;
	}
	if(frameElement && ((e.button||0)==0)) {
		pw.tFrame(frameElement.dictInfo);
	}
	stopLnk = 0;
	nxtEntry = 0;
	withSel = !getSelection().isCollapsed;
	if(t && t.href) { //  && (e.button==1||e.button==0)
		stopLnk = 1;
		debug("01! found link : "+t.href+" : testSound="+regSound.test(t.href));
		var attr = t.getAttribute('href'), href = t.href;
		if(attr.startsWith('#')) { // 标准页内跳转
			t.href=contentUrl+attr; // 转换为contentUrl页内跳转
			stopLnk = t.href;
		}
		else if(attr.startsWith('entry://')) { // entry://跳转
			attr=attr.substring(8);
			if(attr.startsWith('#')) { // entry://#页内跳转
				t.href=contentUrl+attr; // 转换为contentUrl页内跳转
				stopLnk = t.href;
			} else { // entry://词条名跳转
				t._ntz=attr;
				t.href=baseEntry()+attr; // 转换为contentUrl词条跳转 "/base/dOED/entry/词条名";
				entryPop(t);
			}
		} 
		else if(t._ntz) {
			entryPop(t); // 已缓存的词条跳转
		}
		else if(regSound.test(attr)) {/*拦截 sound 连接*/
			baseEntry();
			if(t.onclick==undefined){
				debug("1! found internal sound link : "+attr);
				t.onclick=hiPlaySound;
				//t.addEventListener('click', hiPlaySound)
			}
			t._pd_vc = 1;
			stopLnk = 1;
			return true;
		}
		else if(_app && regHttp.test(t.href)){
			if(!attr.startsWith('#') && attr && attr.trim()) {
				debug('拦截 http');
				stopLnk = t.href;
				return true;
			}
		}
		else if(_app && regPdf.test(t.href)){/*拦截 pdf*/
			_app.handlePdfLink(t.href);
			return false;
		}
		stopLnk = 0;
		debug('连接通行！');
		return true;
	}
}

w._pd_d = wrappedDown;

function wrappedClickFunc(e){
	debug('wrappedClickFunc'+stopLnk+' '+withSel);
	if(pw._touchtarget_lck) {
		stopLnk=nxtEntry=0;
		e.stopPropagation();
		e.preventDefault();
	}
	if(frameElement) {
		pw.postOnClick(frameElement.dictInfo);
	}
	if(_app && stopLnk) {
		if(typeof stopLnk==='string' && !pw._touchtarget_lck) {
			if(!frameElement || stopLnk.startsWith(contentHash))
				location.href = stopLnk;
			else
				_app.handleWebLink(getId(), stopLnk);
		}
		e.preventDefault();
		stopLnk = 0;
		return;
	}
	if(nxtEntry) {
		e.preventDefault();
		if(!frameElement || frameElement.dictInfo.scrollExpand) JumpUrl(nxtEntry);
		nxtEntry=0;
	}
	if(_app && withSel && getSelection().isCollapsed) {
		_app.textMenu(sid.get(), false);
	}
}

function JumpUrl(nxt){
	if(!pw._touchtarget_lck) {
		debug('JumpUrl', nxt);
		var ent=nxt._ntz;
		var h=ent.indexOf('#');
		if(frameElement) {
			debug("JumpUrl::nxtEntry=", nxt.href, location.href);
			if(h>0) {
				pw.JumpUrlTag(frameElement, ent.slice(h+1), nxt.href, nxt);
				//ent=ent.slice(0,h);
			} else {
				pw.JumpUrlFrame(frameElement);
				location.href = nxt.href;
			}
		} else {
			location.href = nxt.href;
		}
	}
}

/*!!!高亮开始*/

})()
</script>